/**
 * 版权所有：厦门市巨龙软件工程有限公司
 * Copyright 2009 Xiamen Dragon Software Eng. Co. Ltd.
 * All right reserved. 
 *====================================================
 * 文件名称: DbAdapter.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2009-12-27			beralee(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 * 
 */
package bera.lee.memorycard;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 *<pre>
 * <b><font color="blue">DbAdapter</font></b>
 * </pre>
 * 
 *<pre>
 * <b>&nbsp;--描述说明--</b>
 * </pre>
 * 
 * <pre></pre>
 * 
 * <pre>
 * <b>--样例--</b>
 *   DbAdapter obj = new DbAdapter();
 *   obj.method();
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * @author <b>beralee</b>
 */
public class DbAdapter {

	public static final String KEY_NAME = "name";

	public static final String KEY_VALUE = "value";

	public static final String KEY_ROWID = "_id";

	private static final String TAG = "MemoryCardDbAdapter";

	private DatabaseHelper mDbHelper;

	private SQLiteDatabase mDb;

	/**
	 * Database creation sql statement
	 */
	private static final String DATABASE_CREATE = "create table beramc (_id integer primary key autoincrement, "
			+ "name text not null, value text not null);";

	private static final String DATABASE_NAME = "data";

	private static final String DATABASE_TABLE = "beramc";

	private static final int DATABASE_VERSION = 2;

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
			// 初始化可玩的最大关卡
			ContentValues initialValues = new ContentValues();
			initialValues.put(KEY_NAME, "maxpoint");
			initialValues.put(KEY_VALUE, "3");
			db.insert(DATABASE_TABLE, null, initialValues);
			// 初始化音乐和音效开关
			initialValues.put(KEY_NAME, "openbgm");
			initialValues.put(KEY_VALUE, "0");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "openmusic");
			initialValues.put(KEY_VALUE, "0");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "point1");
			initialValues.put(KEY_VALUE, "12000");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "point2");
			initialValues.put(KEY_VALUE, "20000");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "point3");
			initialValues.put(KEY_VALUE, "30000");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "point4");
			initialValues.put(KEY_VALUE, "40000");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "point5");
			initialValues.put(KEY_VALUE, "70000");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "point6");
			initialValues.put(KEY_VALUE, "110000");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "point7");
			initialValues.put(KEY_VALUE, "130000");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "point8");
			initialValues.put(KEY_VALUE, "160000");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "callent");
			initialValues.put(KEY_VALUE, "200000");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "canupdate");
			initialValues.put(KEY_VALUE, "0");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "worldscore");
			initialValues.put(KEY_VALUE, "100+");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "iconsize");
			initialValues.put(KEY_VALUE, "48");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "temp1");
			initialValues.put(KEY_VALUE, "");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "temp2");
			initialValues.put(KEY_VALUE, "");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "temp3");
			initialValues.put(KEY_VALUE, "");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "temp4");
			initialValues.put(KEY_VALUE, "");
			db.insert(DATABASE_TABLE, null, initialValues);
			initialValues.put(KEY_NAME, "temp5");
			initialValues.put(KEY_VALUE, "");
			db.insert(DATABASE_TABLE, null, initialValues);

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion
					+ ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS reduction");
			onCreate(db);
		}
	}

	/**
	 * Constructor - takes the context to allow the database to be
	 * opened/created
	 * 
	 * @param ctx
	 *            the Context within which to work
	 */
	public DbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	/**
	 * Open the notes database. If it cannot be opened, try to create a new
	 * instance of the database. If it cannot be created, throw an exception to
	 * signal the failure
	 * 
	 * @return this (self reference, allowing this to be chained in an
	 *         initialization call)
	 * @throws SQLException
	 *             if the database could be neither opened or created
	 */
	public DbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	/**
	 * Create a new note using the title and body provided. If the note is
	 * successfully created return the new rowId for that note, otherwise return
	 * a -1 to indicate failure.
	 * 
	 * @param title
	 *            the title of the note
	 * @param body
	 *            the body of the note
	 * @return rowId or -1 if failed
	 */
	public long createScore(String name, String score) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, name);
		initialValues.put(KEY_VALUE, score);

		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	/**
	 * Delete the note with the given rowId
	 * 
	 * @param rowId
	 *            id of note to delete
	 * @return true if deleted, false otherwise
	 */
	public boolean deleteNote(long rowId) {

		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over the list of all notes in the database
	 * 
	 * @return Cursor over all notes
	 */
	public Cursor fetchAllNotes() {

		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME, KEY_VALUE }, null, null, null, null,
				null);
	}

	/**
	 * Return a Cursor positioned at the note that matches the given rowId
	 * 
	 * @param rowId
	 *            id of note to retrieve
	 * @return Cursor positioned to matching note, if found
	 * @throws SQLException
	 *             if note could not be found/retrieved
	 */
	public Cursor fetchNote(long rowId) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME, KEY_VALUE }, KEY_ROWID + "=" + rowId,
				null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public Cursor fetchValue(String keyname) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_NAME, KEY_VALUE }, KEY_NAME + "='" + keyname
				+ "'", null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}

		return mCursor;

	}

	public boolean updateScore(long rowId, String value) {
		ContentValues args = new ContentValues();
		args.put(KEY_VALUE, value);

		return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public boolean updateValue(String name, String value) {
		ContentValues args = new ContentValues();
		args.put(KEY_VALUE, value);

		return mDb.update(DATABASE_TABLE, args, KEY_NAME + "='" + name + "'", null) > 0;
	}
	public void add4Level(){
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, "point9");
		initialValues.put(KEY_VALUE, "60000");
		mDb.insert(DATABASE_TABLE, null, initialValues);
		// 初始化音乐和音效开关
		initialValues.put(KEY_NAME, "point10");
		initialValues.put(KEY_VALUE, "160000");
		mDb.insert(DATABASE_TABLE, null, initialValues);
		initialValues.put(KEY_NAME, "point11");
		initialValues.put(KEY_VALUE, "160000");
		mDb.insert(DATABASE_TABLE, null, initialValues);
		initialValues.put(KEY_NAME, "point12");
		initialValues.put(KEY_VALUE, "160000");
		mDb.insert(DATABASE_TABLE, null, initialValues);
	}
}
